<?php
namespace Api\Controller;

use Api\Controller\BaseController;

/**
 * 需求类接口
 *
 * @author ryan
 *        
 */
class DemandController extends BaseController
{

    /**
     * 发布需求
     */
    public function addDemand()
    {
        $token = I('token');
        if (empty($token)) {
            $this->result(2000, array(), '未登录');
        }
        $member = D('MemberToken')->where(array(
            'token' => $token
        ))->find();
        if (empty($member)) {
            $this->result(2001, array(), '无权限访问');
        }
        $info = array();
        $info['member_id'] = $member['id'];
        $info['demand_type'] = I('requestType');
        $info['demand_day'] = I('requestDay');
        $info['demand_amount'] = I('requestMoney') * 10000;
        $info['demand_date'] = I('requestDate');
        $info['contacts'] = I('requestContact');
        $info['tel'] = I('requestMobile');
        $info['province'] = I('province');
        $info['city'] = I('city');
        $info['business_name'] = I('businessName');
        $info['recommend_mobile'] = I('recommondMobile');
        if (empty($info['demand_amount'])) {
            $this->result(3000, array(), '需求金额不能为空');
        }
        if (empty($info['contacts'])) {
            $this->result(3000, array(), '联系人姓名不能为空');
        }
        if (empty($info['tel'])) {
            $this->result(3000, array(), '联系电话不能为空');
        }
        if (I('customType') == 1 && empty($info['business_name'])) {
            $this->result(3000, array(), '企业名称不能为空');
        }
        $info['status'] = 10;
        $info['add_time'] = TIMESTAMP;
        $info['demand_sn'] = $this->getSN('DS');
        $status = D('Demand')->add($info);
        if ($status === false) {
            $this->result(3001, array(), '添加失败');
        } else {
            $this->result(0, array(), '添加成功');
        }
    }

    private function getSN($prefix)
    {
        mt_srand((double) microtime() * 1000000);
        return $prefix . date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
    }
}

